//
//  MYXTabBar.swift
//  weibo
//
//  Created by MaYingxin on 16/4/17.
//  Copyright © 2016年 MaYingxin. All rights reserved.
//

import UIKit

protocol MYXTabBarDelegate: NSObjectProtocol {
    
    func composeClick()
}

class MYXTabBar: UITabBar {

    //注意不能和代理名称不能喝协议名称一样
    weak var MYXTarDelegate: MYXTabBarDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        addSubview(composeButton)
//        print(composeButton)
        print(self.subviews.count)
        
//        self.subviews[2].userInteractionEnabled = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func composeClick() -> () {
        
        print("composeClick")
        MYXTarDelegate?.composeClick()
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        print("layoutSubviews:2")
        struct Static{
            
            static var onceToken: dispatch_once_t = 0
        }
        dispatch_once(&Static.onceToken) {
            
            self.subviews[4].removeFromSuperview()
        }
            
//            self.subviews[4].removeFromSuperview()
        
        
//        print(self.subviews[4])
//        print(self.subviews.count)
//        print(self.subviews)
        composeButton.center.x = self.frame.width * 0.5
        composeButton.center.y = self.frame.height * 0.5
    }
    
    
    private lazy var composeButton: UIButton = {
        
        let btn = UIButton()
        
        btn.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        btn.sizeToFit()
        
        self.bringSubviewToFront(btn)
        
        btn.becomeFirstResponder()
        
        return btn
    }()
    

}
